<!DOCTYPE html>
<html>
	<head>
		<link rel="stylesheet" type="text/css" href="../assets/libs/webuploader/webuploader.css" />
		<script type="text/javascript" src="../assets/scripts/jquery/jquery-1.11.2.min.js" ></script>
		<script type="text/javascript" src="../assets/libs/webuploader/webuploader.min.js"></script>
		<title>大文件上传测试</title>
	</head>
	<body>
		<div id="thelist" class="uploader-list"></div>
          <div style="margin:20px 20px 20px 0;">
              <div id="picker" class="form-control-focus">选择文件</div>
          </div>
        <button id="btnSync" type="button" class="btn btn-warning">开始同步</button>
        <script>
        WebUploader.Uploader.register({
            'before-send-file': 'checkfile',
            'before-send':'checkchunk',
            //'after-send-file':'postsend'
        }, {
            checkfile: function( file ) {
                var me = this,
                    owner = this.owner,
                    server = me.options.server,
                    deferred = WebUploader.Deferred();

                owner.md5File( file.source )
	                .progress(function(percentage) {
		                console.log('Percentage:', percentage);
		            })
                	.fail(function() {deferred.reject();})
                    .then(function( md5 ) {
                    	me.options.formData.md5=md5;
                        $.ajax("checkFile", {
                            dataType: 'json',
                            data: {md5: md5},
                            success: function(response) {
                                if (response.exists) owner.skipFile(file);
                                deferred.resolve();
                            }
                        });
                    });
                return deferred.promise();
            },
            checkchunk:function( block ) {
            	var me = this;
            	console.log(arguments);
                var deferred = $.Deferred();
                $.get("checkChunk",{md5:me.options.formData.md5,chunk:block.chunk}).then(function(data) {
                    if (data.exists) {
                    	//block.file.firstChunkNotFound=true;
                        deferred.reject();
                    } else {
                        deferred.resolve();
                    }
                });
	            return deferred.promise();
	        }
        });
        var uploader = WebUploader.create({
            // swf文件路径
            swf : '../assets/libs/webuploader/Uploader.swf',
            // 文件接收服务端。
            server : 'attach',
            chunked : true,
            threads : 1,
            pick : '#picker',
            duplicate:true,
            // 不压缩image, 默认如果是jpeg，文件上传前会压缩一把再上传！
            //resize : false
        });

        // 当有文件被添加进队列的时候
        uploader.on('fileQueued', function(file) {
        	/* uploader.md5File( file ).progress(function(percentage) {
                console.log('Percentage:', percentage);
            }).then(function(val) {
            	uploader.options.formData.md5=val;
                console.log('md5 result:', val);
            }); */
            //alert(123);
            $("#thelist").append('<div id="' + file.id + '" class="item">'
                            + '<h4 class="info">' + file.name + '</h4>'
                            + '<p class="state">等待上传...</p>' + '</div>');
        });

        uploader.on('uploadSuccess', function(file) {
            $('#' + file.id).find('p.state').text('已上传');
        });

        uploader.on('uploadError', function(file) {
            $('#' + file.id).find('p.state').text('上传出错');
        });

        uploader.on('uploadComplete', function(file) {
            $('#' + file.id).find('.progress').fadeOut();
        });

        $("#btnSync").on('click', function() {
            if ($(this).hasClass('disabled')) {
                return false;
            }
            uploader.upload();

        });
        </script>
	</body>
</html>